package org.dromara.system.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.system.domain.dto.WbItemVisitLogsDTO;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.WbItemVisitLogsBo;
import org.dromara.system.domain.vo.WbItemVisitLogsVo;
import org.dromara.system.domain.WbItemVisitLogs;
import org.dromara.system.mapper.WbItemVisitLogsMapper;
import org.dromara.system.service.IWbItemVisitLogsService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 项目浏览记录Service业务层处理
 *
 * @author Lion Li
 * @date 2024-03-22
 */
@RequiredArgsConstructor
@Service
public class WbItemVisitLogsServiceImpl implements IWbItemVisitLogsService {

    private final WbItemVisitLogsMapper baseMapper;

    /**
     * 查询项目浏览记录
     */
    @Override
    public WbItemVisitLogsVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询项目浏览记录列表
     */
    @Override
    public TableDataInfo<WbItemVisitLogsVo> queryPageList(WbItemVisitLogsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WbItemVisitLogs> lqw = buildQueryWrapper(bo);
        lqw.orderByDesc(WbItemVisitLogs::getId);
        Page<WbItemVisitLogsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询项目浏览记录列表
     */
    @Override
    public List<WbItemVisitLogsVo> queryList(WbItemVisitLogsBo bo) {
        LambdaQueryWrapper<WbItemVisitLogs> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WbItemVisitLogs> buildQueryWrapper(WbItemVisitLogsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WbItemVisitLogs> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getItemId() != null, WbItemVisitLogs::getItemId, bo.getItemId());
        lqw.eq(bo.getLoginUser() != null, WbItemVisitLogs::getLoginUser, bo.getLoginUser());
        lqw.eq(bo.getLastBrowseTime() != null,WbItemVisitLogs::getLastBrowseTime,bo.getItemId());
        lqw.orderByDesc(WbItemVisitLogs::getLastBrowseTime);
        return lqw;
    }

    /**
     * 新增项目浏览记录
     */
    @Override
    public Boolean insertByBo(WbItemVisitLogsBo bo) {
        WbItemVisitLogs add = MapstructUtils.convert(bo, WbItemVisitLogs.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改项目浏览记录
     */
    @Override
    public Boolean updateByBo(WbItemVisitLogsBo bo) {
        WbItemVisitLogs update = MapstructUtils.convert(bo, WbItemVisitLogs.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(WbItemVisitLogs entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除项目浏览记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public WbItemVisitLogsVo queryTime(WbItemVisitLogsBo bo) {
        return baseMapper.queryTime(bo);
    }

    @Override
    public List<WbItemVisitLogsDTO> queryGroupTime(Long itemId,PageQuery pageQuery) {
        return baseMapper.queryTimeGroup(itemId,pageQuery);
    }
}
